package com.waidi.clothespad.common;
import android.content.Context;
import android.os.Handler;
import android.util.Log;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.protocol.HTTP;
import org.apache.http.util.EntityUtils;
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

public class HttpGetThread extends Thread
{
    public String TAG = HttpGetThread.class.getName();
    public Context context0 = null;
    public Handler handler0 = null;
    public String  Url = null;
    public int exflag ;
    public String action;
    public String marg[];

    public HttpGetThread(Handler handlerin, Context contextin, String url,int flag,String mAction,String arg[]) {
        handler0 = handlerin;
        context0 = contextin;
        Url = url;
        exflag = flag;
        action = mAction;
        marg = arg;
    }
    public void run()
    {
        try
        {
            String result = "";
            HttpClient httpCient = new DefaultHttpClient();
            httpCient.getParams().setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,5000);//连接时间
            httpCient.getParams().setParameter(CoreConnectionPNames.SO_TIMEOUT,5000);//数据传输时间
            HttpGet httpGet = new HttpGet(Url);
            int code = -1;


            Log.d(TAG,action+"--->");
            Log.i(TAG, Url);
            HttpResponse response = httpCient.execute(httpGet);
            code = response.getStatusLine().getStatusCode();
            Log.i(TAG, "get server reponse code: "+Integer.toString(code));

            if (response.getStatusLine().getStatusCode() == 200)
            {
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity, HTTP.UTF_8);
                Log.i(TAG, result);

                switch (exflag)
                {
                    case 0:
                        cutBedgetStatistics(result);
                        break;
                    case 1:
                        getRelationInfo(result);
                        break;



                    default:
                        break;
                }
            }else
            {
                Log.i(TAG,"服务端返回："+Integer.toString(code));
                new MessageToUi().send(23,"exception","服务端返回："+Integer.toString(code),handler0);
            }
        }
        catch (Exception e)
        {
            Log.e(TAG, "HTTP request err......");
            e.printStackTrace();
            new MessageToUi().send(23,"exception",e.toString(),handler0);
        }
    }

    public void cutBedgetStatistics(String result)
    {
        try
        {
            JSONObject jsonObj = new JSONObject(result);
            if (jsonObj == null)
                return;

            if (jsonObj.has("error") == true)
            {
                Log.i(TAG, "绑定工序错误!");
                JSONObject error = jsonObj.optJSONObject("error");
                String message = error.optString("message");
                String s = "绑定工序错误:" + message;
                new MessageToUi().send(23, "exception", s, handler0);
            }
            else
            {
                if (jsonObj.has("data") == true)
                {
                    String data = jsonObj.optString("data");
                    Log.i(TAG,data);
                    new MessageToUi().send(0, "data", data, handler0);
                }
            }

        }
        catch (JSONException e)
        {
            e.printStackTrace();
            new MessageToUi().send(23,"exception",e.toString(),handler0);
        }
        catch (Exception e)
        {
            Log.e(TAG, "HTTP request err......");
            e.printStackTrace();
            new MessageToUi().send(23,"exception",e.toString(),handler0);
        }
    }

    public void getRelationInfo(String result)
    {
        try
        {
            JSONObject jsonObj = new JSONObject(result);
            if (jsonObj == null)
                return;

            if (jsonObj.has("error") == true)
            {
                Log.i(TAG, "二维码查询款式尺码信息错误!");
                JSONObject error = jsonObj.optJSONObject("error");
                String message = error.optString("message");
                String s = "二维码查询款式尺码信息错误:" + message;
                new MessageToUi().send(23, "exception", s, handler0);
            }
            else
            {
                if (jsonObj.has("data") == true)
                {
                   // String data = jsonObj.optJSONObject("data").toString();

                    //String data = jsonObj.optJSONObject("data").toString();
                    JSONObject data0 = jsonObj.optJSONObject("data");
                    DpsOrderInfo.switchData = data0;
                   // Log.i(TAG,data);
                    new MessageToUi().send(901, "data", marg[0], handler0);

                }
            }

        }
        catch (JSONException e)
        {
            e.printStackTrace();
            new MessageToUi().send(23,"exception",e.toString(),handler0);
        }
        catch (Exception e)
        {
            Log.e(TAG, "HTTP request err......");
            e.printStackTrace();
            new MessageToUi().send(23,"exception",e.toString(),handler0);
        }
    }


}
